package com.me.jingdongshop.ui.holder;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.me.common.base.Global;
import com.me.common.ui.BaseAdapterRV;
import com.me.common.ui.BaseHolderRV;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.Home;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.ui.activity.GoodsDetailActivity;
import com.me.jingdongshop.ui.activity.HomeFlashSaleActivity;
import com.me.jingdongshop.ui.activity.HomeHotItemActivity;
import com.me.jingdongshop.ui.activity.HomeNewProductActivity;
import com.me.jingdongshop.ui.activity.HomePromotionLettersActivity;
import com.me.jingdongshop.ui.activity.HomeRecommendedBrandActivity;
import com.me.jingdongshop.ui.adapter.HomeAdapter;
import com.me.jingdongshop.ui.adapter.HomeRvPicAdapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created by wuer on 2017/5/5/005.
 * 首页界面的所有数据展示
 */

public class HomeHolder extends BaseHolderRV<List<Home.HomeTopicBean>> {
    private SliderLayout slider;
    private PagerIndicator disIndicator;
    private LinearLayout llParent;
    private ImageView category1;
    private ImageView category2;
    private ImageView category3;
    private ImageView category4;
    private ImageView category5;
    private ViewFlipper viewFlipper;
    private TextView tvLastTime;
    private TextView tvGoshop;
    private RecyclerView recyclerView02;
    private SliderLayout slider02;
    private PagerIndicator disIndicator02;
    private Home home;
    private HomeAdapter homeAdapter;
    private HomeRvPicAdapter homeRvPicAdapter;

    private CountDownTimer timer;

    public HomeHolder(Context context, ViewGroup parent, BaseAdapterRV<List<Home.HomeTopicBean>> adapter, int itemType) {
        super(context, parent, adapter, itemType, R.layout.home_rv_items);
    }

    // 查找子控件
    @Override
    public void onFindViews(View itemView) {
        //1.头部轮播图
        slider = (SliderLayout) itemView.findViewById(R.id.slider);
        disIndicator = (PagerIndicator) itemView.findViewById(R.id.dis_indicator);
        //2.专题和品牌
        llParent = (LinearLayout) itemView.findViewById(R.id.ll_parent);
        category1 = (ImageView) itemView.findViewById(R.id.category1);
        category2 = (ImageView) itemView.findViewById(R.id.category2);
        category3 = (ImageView) itemView.findViewById(R.id.category3);
        category4 = (ImageView) itemView.findViewById(R.id.category4);
        category5 = (ImageView) itemView.findViewById(R.id.category5);
        //3.京东热榜
        viewFlipper = (ViewFlipper) itemView.findViewById(R.id.view_flipper);
        viewFlipper.setInAnimation(AnimationUtils.loadAnimation(super.context,
                R.anim.push_up_in));
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(super.context,
                R.anim.push_up_out));
        viewFlipper.startFlipping();  // 开始执行动画
        //4.倒计时
        tvLastTime = (TextView) itemView.findViewById(R.id.tv_last_time);
        timer = new CountDownTimer(7200000, 1000) {  //设置倒计时的时间为2个小时
            @Override
            public void onTick(long millisUntilFinished) {
                SimpleDateFormat hms = new SimpleDateFormat("HH:mm:ss");
                hms.setTimeZone(TimeZone.getTimeZone("GMT"));
                Date tTime = new Date(millisUntilFinished);
                String totalTime = hms.format(tTime);
                tvLastTime.setText(totalTime);
            }

            @Override
            public void onFinish() {
                tvLastTime.setText("onfinish");
                timer.cancel();
            }
        };
        timer.start();

        tvGoshop = (TextView) itemView.findViewById(R.id.tv_goshop);

        //5.商品列表
        recyclerView02 = (RecyclerView) itemView.findViewById(R.id.recycler_view02);
        recyclerView02.setLayoutManager(new GridLayoutManager(
                super.context, 1, GridLayoutManager.HORIZONTAL, false));
        homeRvPicAdapter = new HomeRvPicAdapter();
        //设置适配器
        recyclerView02.setAdapter(homeRvPicAdapter);

        //6.底部轮播图
        slider02 = (SliderLayout) itemView.findViewById(R.id.slider02);
        disIndicator02 = (PagerIndicator) itemView.findViewById(R.id.dis_indicator02);

    }

    // 刷新子控件显示
    @Override
    protected void onRefreshView(final List<Home.HomeTopicBean> bean, final int position) {
        // 1. 显示头部轮播图
        showHeadLooperImage(bean);

        // 2. 专题和品牌
        showTopicSale();

        // 3. 显示京东热榜数据(ViewFlipper)
        showViewFlipper(bean);

        // 6. 显示底部轮播图（网络上获取图片）
        //showFooterLooperImage(bean);
        //7.显示底部轮播图（本地上获取图片）
        showFooterLooperImage2();

        tvGoshop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), GoodsDetailActivity.class);
                intent.putExtra("id",bean.get(position).getId());
                v.getContext().startActivity(intent);
            }
        });
    }


    // 1. 显示头部轮播图
    private void showHeadLooperImage(List<Home.HomeTopicBean> homeTopic) {
        // 删除之前的子界面
        slider.removeAllSliders();
        // 添加多个轮播的子界面
        for (Home.HomeTopicBean bean : homeTopic) {
            TextSliderView sliderView = new TextSliderView(super.context);
            // 指定图片和标题, 拼接ip地址
            String imageUrl = IHttpService.HOST_URL + bean.getPic();
            sliderView.description(bean.getTitle()).image(imageUrl);
            // 添加子界面
            slider.addSlider(sliderView);
            // 设置点击事件
            sliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                @Override
                public void onSliderClick(BaseSliderView slider) {
                    Global.showToast("点击：" + slider.getDescription());
                }
            });
        }
    }

    // 2. 专题和品牌
    private void showTopicSale() {
        category1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomeFlashSaleActivity.class);//限时抢购
                context.startActivity(intent);
                return;
            }
        });

        category2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomePromotionLettersActivity.class);//促销快报
                context.startActivity(intent);
                return;
            }
        });
        category3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomeNewProductActivity.class);//新品上架
                context.startActivity(intent);
                return;
            }
        });


        category4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, HomeHotItemActivity.class);//热门单品
                context.startActivity(intent);
                return;
            }
        });

        category5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == category5) {
                    Intent intent = new Intent(context, HomeRecommendedBrandActivity.class);//推荐品牌
                    context.startActivity(intent);
                    return;
                }
            }
        });
    }

    // 3. 显示京东热榜数据(ViewFlipper)
    private void showViewFlipper(List<Home.HomeTopicBean> homeTopic) {
        viewFlipper.removeAllViews();   // 删除之前的item
        for (Home.HomeTopicBean bean : homeTopic) {
            TextView textView = (TextView) Global.inflate(
                    R.layout.item_view_flipper, viewFlipper);
            textView.setText(bean.getTitle());
            viewFlipper.addView(textView);
        }
    }

    // 6. 显示底部轮播图（网络上获取图片）
    private void showFooterLooperImage(List<Home.HomeTopicBean> homeTopic) {
        // 删除之前的子界面
        slider02.removeAllSliders();
        // 添加多个轮播的子界面
        for (Home.HomeTopicBean bean : homeTopic) {
            TextSliderView sliderView = new TextSliderView(super.context);
            // 指定图片和标题, 拼接ip地址
            String imageUrl = IHttpService.HOST_URL + bean.getPic();
            sliderView.description(bean.getTitle()).image(imageUrl);
            // 添加子界面
            slider02.addSlider(sliderView);
            // 设置点击事件
            sliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                @Override
                public void onSliderClick(BaseSliderView slider) {
                    Global.showToast("点击：" + slider.getDescription());
                }
            });
        }
    }

    int[] slidePic = {R.drawable.top_show_pic1, R.drawable.top_show_pic1,
            R.drawable.top_show_pic2, R.drawable.top_show_pic3,
            R.drawable.top_show_pic4, R.drawable.top_show_pic5,
            R.drawable.top_show_pic6, R.drawable.top_show_pic7};

    //7.显示底部轮播图（本地上获取图片）
    private void showFooterLooperImage2() {
        // 删除之前的子界面
        slider02.removeAllSliders();
        for (int i = 0; i < slidePic.length; i++) {
            TextSliderView sliderView = new TextSliderView(super.context);
            sliderView.image(slidePic[i]);
            slider02.addSlider(sliderView);
        }
    }

}
